<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/9/28
 * Time: 15:02
 */

namespace app\index\controller;
use app\index\model\Shop as ShopModel;
use think\Db;

class Single
{
    /**
     * 左侧一级类
     * @return array
     */
    public function left_one(){
        $data = Db::name('single')->where('auding_status',2)->where('parent_id',0)
            ->where('is_delete',1)
            ->field('id,cat_name,pic_url')
            ->order('id','asc')
            ->select();
        return ['code' => 200 ,'message' => '左侧汽车服务1级分类','data' => $data];
    }

    public function details($id, $member_id = 0,$area_name = '哈尔滨', $longitude = '46.7731912817', $latitude = '126.6311421704')
    {
        /* 会员登录了
         * 当前会员评价不需要审核条件
         * $where['member_id'] = ['eq',$member_id];
         * function($query){
        $query->whereOr('member_id',$member_id)->whereOr(' ');
    }
         *
         */
        try {
            $single_find = Db::name('single')->where('id', $id)->field('cat_name,pic_url')->find();
            $others_evaluate = [];
            $w = '';
            if (!empty($member_id)) {
                $auding_status['single_id'] = ['eq', $id];
                $auding_status['oe.member_id'] = ['eq', $member_id];
                $others_evaluate = function ($query) use ($id) {
                    $query->where('auding_status', 'eq', '2')->where('single_id', 'eq', $id);
                };

                $evaluate_list = Db::name('order_evaluate oe')
                    ->join('member m', 'oe.member_id = m.id', 'left')
                    ->where($auding_status)->whereOr($others_evaluate)
                    ->where('order_type', 2)
                    ->field('oe.*,m.username,m.head_pic')
                    ->order('oe.id', 'desc')
                    ->paginate(3)->toArray();

                foreach ($evaluate_list['data'] as $k => &$v) {
                    $v['username'] = hideStr($v['username'], 3, 4);
                }
                $evaluate_count = Db::name('order_evaluate oe')
                    ->where($auding_status)->whereOr($others_evaluate)
                    ->count('id');

            } else {
                $evaluate_list = Db::name('order_evaluate oe')
                    ->join('member m', 'oe.member_id = m.id')
                    ->where('single_id', $id)
                    ->where(['auding_status' => 2])
                    ->where('order_type', 2)
                    ->field('oe.*,m.username,m.head_pic')
                    ->order('oe.id', 'desc')
                    ->paginate(3)->toArray();
                foreach ($evaluate_list['data'] as $k => &$v) {
                    $v['username'] = hideStr($v['username'], 3, 4);
                }
                $evaluate_count = Db::name('order_evaluate oe')
                    ->where('single_id', $id)
                    ->where('order_type', 2)
                    ->where(['auding_status' => 2])
                    ->count('id');
            }
            //$shop_data = http_gets('http://lihai.xmdonkey.com/index.php/index/shop/index/area_name/'.$area_name.'/longitude/'.$longitude.'/latitude/'.$latitude);
            $area_id = Db::name('area')->where('area_name', 'like', '%' . $area_name . '%')->value('area_id');
            $shop_list =  (new ShopModel())
                ->where('auding_status', 2)
//            ->where('city_id', $area_id)
                ->where('is_delete', 1)
                ->where('status', 1)
                ->field('id,shop_title shop_name,thumbnail,thumbnail pic_url,shop_address,sale_number,longitude,latitude')->paginate(600)->each(function ($item, $key) use ($longitude, $latitude) {
                    //$data = http_gets('https://restapi.amap.com/v3/direction/walking?origin=116.434307,39.90909&destination=116.434446,39.90816&key=fe1e97dc6d56d758cf297a8ee7ecf0f2');
                    ///halt(json_decode($data,true)['route']['paths']['distance']);
                    $data = http_gets('https://restapi.amap.com/v3/direction/walking?origin=116.434307,39.90909&destination='.$item['longitude'].','.$item['latitude'].'&key=fe1e97dc6d56d758cf297a8ee7ecf0f2');
                    ///halt(json_decode($data,true)['route']['paths']['distance']);
                    $url = 'https://restapi.amap.com/v3/direction/walking?origin='.$longitude.','.$latitude.'&destination='.$item['longitude'].','.$item['latitude'].'&key=fe1e97dc6d56d758cf297a8ee7ecf0f2';
                    $data = (new Shop())->distance($latitude,$longitude,$item['latitude'],$item['longitude']);
                    $item->distance = $data;
                    //$item->distance = round(getDistance($item['longitude'], $item['latitude'], (float)$longitude, (float)$latitude), 1);
                    $item->evaluate_count = Db::name('order_evaluate')->where('shop_id', $item->id)->where('auding_status',2)->count();
                });
            return ['code' => 200, 'message' => '服务类图片和评价数据', 'single_find' => $single_find, 'evaluate_count' => $evaluate_count, 'evaluate_list' => $evaluate_list,'shop_list' => $shop_list->toArray()['data']];
        } catch (\Exception $e) {
            return ['code' => $e->getCode(), 'message' => $e->getMessage(), 'line' => $e->getLine()];
        }
    }

    /**
     * like....
     * 右侧2级类
     * @param $id
     * @return array
     */
    public function right_two($id){
        $data = Db::name('single')->where('auding_status',2)->where('parent_id',0)
            ->where('is_delete',1)
            ->field('id,cat_name,pic_url,jump_link,parent_id')
            ->where('parent_id',$id)
            ->select()->toArray();
        foreach ($data as &$v){
            $v['cat_name'] = str_len($v['cat_name'],9,'');
        }
        $single_name = Db::name('single')->where('id',$id)->value('cat_name');

        $ad_banner = Db::name('banner')->fetchSql(false)->where('auding_status',2)->where('is_delete',1)->field('pic_url,jump_link')->where('title','like','%'.$single_name.'%')->find();

        if(is_null($ad_banner)) $ad_banner = ['pic_url' => '','jump_link' => ''];
        $type_id = Db::name('single')->where('id',$id)->value('id');
        $type_name = Db::name('single')->where('id',$id)->value('cat_name');
        return ['code' => 200 ,'message' => '右侧汽车服务2级分类','data' => $data,'ad_banner' => $ad_banner,'type_id' => $type_id ,'type_name' => $type_name];
    }

    public function view($id)
    {
        $content = '<style> img {vertical-align:middle; width:auto; max-width:95%;} </style>';
        $content .= '<meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">';
        $content .= Db::name('single')->where('id', $id)->value('content');
        return response($content);
    }

    public function enquiry(){
        $ret = Db::name('web')->where('id',7)->value('jump_link');
        return ['code' =>200,'message' => '违章查询网站','data' => $ret];
    }

    public function supermarket(){
        $ret = Db::name('web')->where('id',8)->value('jump_link');
        return ['code' =>200,'message' => '汽车超市','data' => $ret];
    }

    public function safe(){

    }

    public function year(){

    }
}